<%@ page import="com.minxi.jsp.utils.DruidUtil" %>
<%@ page import="com.minxi.jsp.javabean.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.util.Date" %>
<%--
  Created by IntelliJ IDEA.
  User: 23738
  Date: 2024/6/12
  Time: 21:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>查询内容如下：</title>
    <link rel="stylesheet" type="text/css" href="../../css/2024-06-06_new.css">
</head>
<%!
    public static ArrayList<User> users;

    public static boolean select(String UserName, Integer Age) {
        // 定义null值
        PreparedStatement st = null;
        Connection conn = null;
        ResultSet rs = null;
        users = new ArrayList<>();
        try {

            conn = DruidUtil.DruidConnect();
            // 4.定义sql（预编译）
            String sql = "select * from user_info where user_name = ? and user_age = ?;";

            // 5.获取sql对象
            st = conn.prepareStatement(sql);
            st.setString(1, UserName);
            st.setInt(2, Age);

            // 6.执行sql语句
            rs = st.executeQuery();
            while (rs.next()) {
                // 把从数据库拿到的数据存起来
                int id = rs.getInt("id");
                String userName = rs.getString("user_name");
                String passWord = rs.getString("user_pwd");
                int age = rs.getInt("user_age");
                int sex = rs.getInt("user_sex");
                String userPhone = rs.getString("user_phone");
                String userAddress = rs.getString("user_address");
                Date userTime = rs.getDate("user_time");
                int roleType = rs.getInt("role_type");

                // 用拿到的数据创建用户对象
                User user = new User();
                user.setId(id);
                user.setUserName(userName);
                user.setPassWord(passWord);
                user.setAge(age);
                user.setSex(sex);
                user.setUserPhone(userPhone);
                user.setUserAddress(userAddress);
                user.setUserTime(userTime);
                user.setRoleType(roleType);

                // 往用户列表里添加用户对象
                users.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                DruidUtil.DruidClose(rs, st, conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (!users.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
%>
<body>
<%
    try {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        Integer age = Integer.parseInt(request.getParameter("age"));

        if (!select(username, age)) {
            out.print("<script type=\"text/javascript\">" +
                    "alert(\"抱歉，未查询到指定用户~\")\n" +
                    "window.location.href = \"index2.jsp\";" +
                    "</script>");
        } else {
            for (User user : users) {%>

<div class="nev">
    <form>
        <input type="text" name="userName" placeholder="请输入用户名" value="<%=user.getUserName()%>" disabled="disabled">
        <input type="password" name="userPwd" placeholder="请输入用户密码" value="<%=user.getPassWord()%>" disabled="disabled">
        <input type="text" name="userAge" placeholder="年龄" value="<%=user.getAge()%>" disabled="disabled">
        <label><input type="radio" name="userSex" class="key" value="1" <%=user.getSex()==1?"checked":""%> disabled="disabled">男</label>
        <label><input type="radio" name="userSex" class="key" value="2" <%=user.getSex()!=1?"checked":""%> disabled="disabled">女</label><br>
        <input type="text" name="userPhone" placeholder="请输入手机号码" value="<%=user.getUserPhone()%>" disabled="disabled">
        <input type="text" name="userAddress" placeholder="请输入所在城市" value="<%=user.getUserAddress()%>" disabled="disabled">
        <input type="date" name="userTime" placeholder="请输入出生日期" value="<%=user.getUserTime()%>" disabled="disabled">
        <label><input type="radio" name="roleType" class="key" value="1" <%=user.getRoleType()==1?"checked":""%> disabled="disabled">老师</label>
        <label><input type="radio" name="roleType" class="key" value="2" <%=user.getRoleType()!=1?"checked":""%> disabled="disabled">学生</label><br>
        <input type="submit" value="提交" id="submit" disabled="disabled">
        <input type="reset" value="清空" id="reset" disabled="disabled">
    </form>
</div>

<% }
}
} catch (Exception e) {
    e.printStackTrace();
}
%>
</body>
</html>
